﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Web.Module
{
    public partial class SanphamTop : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                listSP.DataSource = BLL.SanphamBLL.Sanpham_GetByTop("", "", "");
                listSP.DataBind();
            }
        }

        public string FormatGia(string value)
        {
            value = value.Replace(",", "");
            value = value.Replace(".", "");
            string s = "";
            while (value.Length > 3)
            {
                s = "." + value.Substring(value.Length - 3) + s;
                value = value.Substring(0, value.Length - 3);
            }
            s = value + s + " VND";
            return s;
        }

        protected void listSP_ItemCommand(object source, DataListCommandEventArgs e)
        {
            DataTable cart;
            DataTable product;
            string str = e.CommandArgument.ToString();
            if (Session["cart"] == null)
            {
                cart = new DataTable();
                cart.Columns.Add("Ma_San_pham", typeof(string));
                cart.Columns.Add("Ten_San_pham", typeof(string));
                cart.Columns.Add("So_luong", typeof(string));
                cart.Columns.Add("Gia_ban", typeof(string));
                product = BLL.SanphamBLL.Sanpham_GetById(str);
                DataRow row = cart.NewRow();
                row["Ma_San_pham"] = str;
                row["Ten_San_pham"] = product.Rows[0]["Ten_San_pham"].ToString();
                row["So_luong"] = 1;
                row["Gia_ban"] = product.Rows[0]["Gia_ban"].ToString();
                cart.Rows.Add(row);
            }
            else
            {
                cart = (DataTable)Session["cart"];
                product = BLL.SanphamBLL.Sanpham_GetById(str);
                int dem = 0;
                int a = 0;
                for (int i = 0; i < cart.Rows.Count; i++)
                {
                    if (cart.Rows[i]["Ma_San_pham"].ToString() == str)
                    {
                        dem = int.Parse(cart.Rows[i]["So_luong"].ToString());
                        a = i;
                        break;
                    }
                }
                DataRow row = cart.NewRow();
                row["Ma_San_pham"] = str;
                row["Ten_San_pham"] = product.Rows[0]["Ten_San_pham"].ToString();
                if (dem != 0)
                {
                    row["So_luong"] = dem + 1;
                    cart.Rows.RemoveAt(a);
                }
                else
                {
                    row["So_luong"] = 1;
                }
                row["Gia_ban"] = product.Rows[0]["Gia_ban"].ToString();
                cart.Rows.Add(row);
            }
            Session["cart"] = cart;
        }
    }
}